import { APIs } from '@/Http/http'

// AI客服相关的接口类型定义
export interface ChatMessage {
  sessionId?: string
  userMessage: string
  userName?: string
  messageType?: string
  clientId?: string
  deviceInfo?: string
}

export interface ConversationHistory {
  sessionId: string
  userId: string
  startTime: string
  endTime: string
  pageIndex: number
  pageSize: number
}

export interface RateSatisfaction {
  conversationId: number
  satisfactionRating: number
  feedback: string
  isResolved: boolean
}

export interface Knowledge {
  title: string
  question: string
  answer: string
  category: string
  tags: string
  priority: number
  matchThreshold: number
  relatedLinks: string
}

export interface UpdateKnowledge extends Knowledge {
  knowledgeId: number
}

// AI客服服务类
export class AICustomerService {
  
  /**
   * 发送聊天消息
   */
  static async chat(message: ChatMessage) {
    try {
      const response = await APIs.post('/api/AICustomerService/Chat', message)
      return response.data
    } catch (error) {
      console.error('发送消息失败:', error)
      throw error
    }
  }

  /**
   * 获取对话历史
   */
  static async getConversationHistory(params: {
    sessionId?: string
    userId?: string
    startTime?: string
    endTime?: string
    pageIndex?: number
    pageSize?: number
  }) {
    try {
      const response = await APIs.get('/api/AICustomerService/GetConversationHistory', {
        params
      })
      return response.data
    } catch (error) {
      console.error('获取对话历史失败:', error)
      throw error
    }
  }

  /**
   * 满意度评价
   */
  static async rateSatisfaction(rating: RateSatisfaction) {
    try {
      const response = await APIs.post('/api/AICustomerService/RateSatisfaction', rating)
      return response.data
    } catch (error) {
      console.error('满意度评价失败:', error)
      throw error
    }
  }

  /**
   * 关闭对话
   */
  static async closeConversation(closeReason: string) {
    try {
      const response = await APIs.post('/api/AICustomerService/CloseConversation', closeReason)
      return response.data
    } catch (error) {
      console.error('关闭对话失败:', error)
      throw error
    }
  }

  /**
   * 创建知识库条目
   */
  static async createKnowledge(knowledge: Knowledge) {
    try {
      const response = await APIs.post('/api/AICustomerService/CreateKnowledge', knowledge)
      return response.data
    } catch (error) {
      console.error('创建知识库条目失败:', error)
      throw error
    }
  }

  /**
   * 更新知识库条目
   */
  static async updateKnowledge(knowledge: UpdateKnowledge) {
    try {
      const response = await APIs.put('/api/AICustomerService/UpdateKnowledge', knowledge)
      return response.data
    } catch (error) {
      console.error('更新知识库条目失败:', error)
      throw error
    }
  }

  /**
   * 删除知识库条目
   */
  static async deleteKnowledge(knowledgeId: number) {
    try {
      const response = await APIs.delete('/api/AICustomerService/DeleteKnowledge', {
        params: { knowledgeId }
      })
      return response.data
    } catch (error) {
      console.error('删除知识库条目失败:', error)
      throw error
    }
  }

  /**
   * 查询知识库
   */
  static async queryKnowledge(params: {
    keyword?: string
    category?: string
    isEnabled?: boolean
    pageIndex?: number
    pageSize?: number
  }) {
    try {
      const response = await APIs.post('/api/AICustomerService/QueryKnowledge', params)
      return response.data
    } catch (error) {
      console.error('查询知识库失败:', error)
      throw error
    }
  }

  /**
   * 切换知识库状态
   */
  static async toggleKnowledgeStatus(knowledgeId: number, isEnabled: boolean) {
    try {
      const response = await APIs.patch('/api/AICustomerService/ToggleKnowledgeStatus', {
        knowledgeId,
        isEnabled
      })
      return response.data
    } catch (error) {
      console.error('切换知识库状态失败:', error)
      throw error
    }
  }

  /**
   * 搜索知识库
   */
  static async searchKnowledge(params: {
    keyword: string
    matchThreshold?: number
    limit?: number
  }) {
    try {
      const response = await APIs.get('/api/AICustomerService/SearchKnowledge', {
        params
      })
      return response.data
    } catch (error) {
      console.error('搜索知识库失败:', error)
      throw error
    }
  }
}



